<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="行政区划编码" prop="adminCode">
      <el-input v-model="dataForm.adminCode" placeholder="行政区划编码"></el-input>
    </el-form-item>
    <el-form-item label="客户姓名" prop="customerName">
      <el-input v-model="dataForm.customerName" placeholder="客户姓名"></el-input>
    </el-form-item>
    <el-form-item label="客户来源" prop="customerSource">
      <el-input v-model="dataForm.customerSource" placeholder="客户来源"></el-input>
    </el-form-item>
    <el-form-item label="客户电话" prop="customerPhone">
      <el-input v-model="dataForm.customerPhone" placeholder="客户电话"></el-input>
    </el-form-item>
    <el-form-item label="客户需求" prop="customerRequirements">
      <el-input v-model="dataForm.customerRequirements" placeholder="客户需求"></el-input>
    </el-form-item>
    <el-form-item label="预约时间" prop="appointmentTime">
      <el-input v-model="dataForm.appointmentTime" placeholder="预约时间"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="预约地址" prop="appointmentLocation">
      <el-input v-model="dataForm.appointmentLocation" placeholder="预约地址"></el-input>
    </el-form-item>
    <el-form-item label="订单状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="订单状态"></el-input>
    </el-form-item>
    <el-form-item label="审核状态" prop="auditStatus">
      <el-input v-model="dataForm.auditStatus" placeholder="审核状态"></el-input>
    </el-form-item>
    <el-form-item label="审核描述" prop="auditRemark">
      <el-input v-model="dataForm.auditRemark" placeholder="审核描述"></el-input>
    </el-form-item>
    <el-form-item label="客户人员编号" prop="staffId">
      <el-input v-model="dataForm.staffId" placeholder="客户人员编号"></el-input>
    </el-form-item>
    <el-form-item label="客户人员姓名" prop="staffName">
      <el-input v-model="dataForm.staffName" placeholder="客户人员姓名"></el-input>
    </el-form-item>
    <el-form-item label="接单人编号" prop="orderTakerId">
      <el-input v-model="dataForm.orderTakerId" placeholder="接单人编号"></el-input>
    </el-form-item>
    <el-form-item label="接单人姓名" prop="orderTakerName">
      <el-input v-model="dataForm.orderTakerName" placeholder="接单人姓名"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          adminCode: '',
          customerName: '',
          customerSource: '',
          customerPhone: '',
          customerRequirements: '',
          appointmentTime: '',
          createTime: '',
          appointmentLocation: '',
          status: '',
          auditStatus: '',
          auditRemark: '',
          staffId: '',
          staffName: '',
          orderTakerId: '',
          orderTakerName: '',
          remark: ''
        },
        dataRule: {
          adminCode: [
            { required: true, message: '行政区划编码不能为空', trigger: 'blur' }
          ],
          customerName: [
            { required: true, message: '客户姓名不能为空', trigger: 'blur' }
          ],
          customerSource: [
            { required: true, message: '客户来源不能为空', trigger: 'blur' }
          ],
          customerPhone: [
            { required: true, message: '客户电话不能为空', trigger: 'blur' }
          ],
          customerRequirements: [
            { required: true, message: '客户需求不能为空', trigger: 'blur' }
          ],
          appointmentTime: [
            { required: true, message: '预约时间不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          appointmentLocation: [
            { required: true, message: '预约地址不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '订单状态不能为空', trigger: 'blur' }
          ],
          auditStatus: [
            { required: true, message: '审核状态不能为空', trigger: 'blur' }
          ],
          auditRemark: [
            { required: true, message: '审核描述不能为空', trigger: 'blur' }
          ],
          staffId: [
            { required: true, message: '客户人员编号不能为空', trigger: 'blur' }
          ],
          staffName: [
            { required: true, message: '客户人员姓名不能为空', trigger: 'blur' }
          ],
          orderTakerId: [
            { required: true, message: '接单人编号不能为空', trigger: 'blur' }
          ],
          orderTakerName: [
            { required: true, message: '接单人姓名不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/quickService/order/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.adminCode = data.order.adminCode
                this.dataForm.customerName = data.order.customerName
                this.dataForm.customerSource = data.order.customerSource
                this.dataForm.customerPhone = data.order.customerPhone
                this.dataForm.customerRequirements = data.order.customerRequirements
                this.dataForm.appointmentTime = data.order.appointmentTime
                this.dataForm.createTime = data.order.createTime
                this.dataForm.appointmentLocation = data.order.appointmentLocation
                this.dataForm.status = data.order.status
                this.dataForm.auditStatus = data.order.auditStatus
                this.dataForm.auditRemark = data.order.auditRemark
                this.dataForm.staffId = data.order.staffId
                this.dataForm.staffName = data.order.staffName
                this.dataForm.orderTakerId = data.order.orderTakerId
                this.dataForm.orderTakerName = data.order.orderTakerName
                this.dataForm.remark = data.order.remark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/quickService/order/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'adminCode': this.dataForm.adminCode,
                'customerName': this.dataForm.customerName,
                'customerSource': this.dataForm.customerSource,
                'customerPhone': this.dataForm.customerPhone,
                'customerRequirements': this.dataForm.customerRequirements,
                'appointmentTime': this.dataForm.appointmentTime,
                'createTime': this.dataForm.createTime,
                'appointmentLocation': this.dataForm.appointmentLocation,
                'status': this.dataForm.status,
                'auditStatus': this.dataForm.auditStatus,
                'auditRemark': this.dataForm.auditRemark,
                'staffId': this.dataForm.staffId,
                'staffName': this.dataForm.staffName,
                'orderTakerId': this.dataForm.orderTakerId,
                'orderTakerName': this.dataForm.orderTakerName,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
